﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.IO;

namespace Triples
{
    class Triple:IComparable<Triple> , ICloneable
    {
        private int a;
        private int b;
        private int c;

        public Triple(int a, int b, int c)
        {
            this.a = a;
            this.b = b;
            this.c = c;
        }

        public int A
        {
            get
            {
                return a;
            }
            set
            {
                a = value;
            }
        }

        public int B
        {
            get
            {
                return b;
            }
            set
            {
                a = value;
            }
        }

        public int C
        {
            get
            {
                return c;
            }
            set
            {
                a = value;
            }
        }

        public override bool Equals(object obj)
        {
            return this.A.Equals(((Triple)obj).A) && this.B.Equals(((Triple)obj).B) && this.C.Equals(((Triple)obj).C);
        }

        public static Triple operator +(Triple first, Triple second)
        {
            int sumA = first.a + second.a;
            int sumB = first.b + second.b;
            int sumC = first.c + second.c;

            return new Triple(sumA, sumB, sumC);
        }

        public static Triple operator -(Triple first, Triple second)
        {
            int diffA = first.a - second.a;
            int diffB = first.b - second.b;
            int diffC = first.c - second.c;

            return new Triple(diffA, diffB, diffC);
        }

        public static Triple operator ++(Triple first)
        {
            first.a += 1;
            first.b += 1;
            first.c += 1;

            return first;
        }

        public int SumComponents()
        {
            return this.a + this.b + this.c;
        }

        public override string ToString()
        {
            return string.Format("({0}, {1}, {2})", a, b, c);
        }

        public int CompareTo(Triple other)
        {
            int currentObjectSum = this.SumComponents();
            int comparingObjectSum = other.SumComponents();

            return currentObjectSum.CompareTo(comparingObjectSum);
        }

        public object Clone()
        {
            return new Triple(this.a, this.b, this.c);
        }


        public static Match FindAllTriplesInText(string filename)
        {
            string pattern = @"\(\s*(0|(-?[1-9][0-9]*))\s*,\s*(0|(-?[1-9][0-9]*))\s*,\s*(0|(-?[1-9][0-9]*))\)";

            StreamReader inputFile = new StreamReader(filename);
            string text = string.Empty;
            using (inputFile)
            {
                text = inputFile.ReadToEnd();
            }

            return Regex.Match(text, pattern);
        }

    }
}
